const mongoose = require('mongoose');
const FoodSchema = require('../schema/food.schema.js');
const MerchantSchema = require('../schema/merchant.schema');
const FoodModel = mongoose.model('Food', FoodSchema);
const MerchantModel = mongoose.model('Merchant', MerchantSchema);

exports.insertFood = function (food, callback) {
    console.log('[dao 插入食物]');
    let f = new FoodModel(food);
    f.save(food, function (err, data) {
        if (err) {
            console.log(err);
            let message = [
                err.errors.foodPrice != undefined ? err.errors.foodPrice.message : '',
                err.errors.foodName != undefined ? err.errors.foodName.message : ''
            ];
            callback({ status: false, data: {}, message: message });
        } else {
            callback({ status: true, data: data, message: '' });
        }
     
    });
}

exports.deleteFood = function (id, callback) {

    FoodModel.remove({ _id: id }, function (err) {
        if (err) {
            console.log(err);
            callback({ status: false, message: '服务器异常' });
        } else {
            callback({ status: true });
        }
    });
}

exports.updateFood = function (food, callback) {
    console.log('[dao修改]', food);
    FoodModel.update({ _id: food._id }, { $set: food }, function (err, data) {
        if (err) {
            console.log(err);
            callback({ status: false, data: {}, message: '修改失败' });
        } else {
            callback({ status: true, data: data, message: '' });
        }
    });
}
exports.findAll = function (callback) {
    FoodModel.find({}, function (err, data) {
        if (err) {
            console.log(err);
            callback({ status: false, data: {}, message: '服务器异常' });
        } else {
            callback({ status: true, data: data, message: '' });
        }
    });
}
exports.findById = function (id, callback) {
    FoodModel.findOne({ _id: id }, function (err, data) {
        if (err) {
            console.log(err);
            callback({ status: false, data: {}, message: '服务器异常' });
        } else {
            callback({ status: true, data: data, message: '' });
        }
    });
}
//根据IDS 查找食物列表
exports.findByIds = function (ids, callback) {
    FoodModel.find({ _id: { $in: ids } })
        .populate('merchant', ['_id', 'merchantName', 'address'])
        .exec((err, data) => {
            if (err) {
                console.log(err);
                callback({ status: false, data: {}, message: '服务器异常' });
            } else {
                callback({ status: true, data: data, message: '' });
            }
        });
}
// 根据foodtype来查找所有商家
exports.findMerchantByFoodtypeId = function (id, callback) {
    FoodModel.find({ foodType: id })
        .populate('merchant', ['_id', 'merchantName', 'address', 'idCardPic', 'headPic'])
        .exec((err, data) => {
            if (err) {
                console.log(err);
                callback({ status: false, data: {}, message: '服务器异常' });
            } else {
                let result = [];
                let set = new Set();
                console.log('[All]', data);
                for (let i of data) {
                    console.log('[H][H][H][H][H][H][H][H][H][H]')
                    console.log(i);
                    if (i.merchant) {
                        let id = i.merchant._id;
                        if (!set.has(id)) {
                            set.add(id);
                            result.push(i.merchant);
                        }
                    }
                }
                callback({ status: true, data: result, message: '' });
            }
        });
}
exports.findByMId = function (m_id, callback) {
    FoodModel.find({ merchant: m_id }, function (err, data) {
        if (err) {
            console.log(err);
            callback({ status: false, data: {}, message: '服务器异常' });
        } else {
            callback({ status: true, data: data, message: '' });
        }
    });
}

exports.findByMIdandFTId = function (m_id, foodType_id, callback) {
    FoodModel.find({ merchant: m_id, foodType: foodType_id }, function (err, data) {
        if (err) {
            console.log(err);
            callback({ status: false, data: {}, message: '服务器异常' });
        } else {
            callback({ status: true, data: data, message: '' });
        }
    });
}

exports.findByName = function (foodName, callback) {
    FoodModel.find({ foodName: foodName }, function (err, data) {
        if (err) {
            console.log(err);
            callback({ status: false, data: {}, message: '服务器异常' });
        } else {
            callback({ status: true, data: data, message: '' });
        }
    });
}

exports.findByType = function (foodType, callback) {
    FoodModel.find({ foodType: foodType }, function (err, data) {
        if (err) {
            console.log(err);
            callback({ status: false, data: {}, message: '服务器异常' });
        } else {
            callback({ status: true, data: data, message: '' });
        }
    });
}


